//
//  LWKLocationAlertView.swift
//  ljy_demo
//
//  Created by xiandao on 2025/9/4.
//

import UIKit
import SnapKit

protocol LWKLocationAlertViewDelegate : AnyObject {
    func locationViewClose() // 关闭弹窗
}

class LWKLocationAlertView: UIView, LWKFont {
    weak var delegate : LWKLocationAlertViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    func setupUI() {
        self.addSubview(maskBgView)
        maskBgView.addSubview(contentBgView)
        contentBgView.addSubview(titleLab)
        contentBgView.addSubview(msgLab)
        contentBgView.addSubview(openLocationBtn)
        maskBgView.addSubview(closeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        maskBgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentBgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreen_width - CGFloat(100))
            make.height.equalTo(386)
        }
        
        titleLab.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.left.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(25)
        }
        
        msgLab.snp.makeConstraints { make in
            make.top.equalTo(titleLab.snp.bottom).offset(6)
            make.left.equalTo(32)
            make.right.equalTo(-28)
        }
        
        openLocationBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-24)
            make.width.equalTo(kScreen_width - CGFloat(150))
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
        }
        
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(contentBgView.snp.bottom).offset(16)
            make.width.height.equalTo(32)
            make.centerX.equalTo(contentBgView)
        }
    }
    
    //MARK: 懒加载
    // 背景灰色蒙层
    lazy var maskBgView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.hexColor(hex: "#000000", alpha: 0.2)
        return view
    }()
    
    // 权限弹框主内容
    lazy var contentBgView : UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "location_alert_bg")
        imgView.contentMode = .scaleToFill
        imgView.isUserInteractionEnabled = true
        return imgView
    }()
    
    lazy var titleLab : UILabel = {
        let lab = UILabel()
        lab.text = "享受全天候不间断智能服务"
        lab.textAlignment = .center
        lab.font = lwkA3Font()
        lab.textColor = lwkA3FontColor()
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var msgLab : UILabel = {
        let lab = UILabel()
        lab.text = "更好的体验智能眼镜服务\n保持眼镜与李未可GO实时互联\n允许App获取“始终允许”定位权限"
        lab.textAlignment = .center
        lab.font = lwkA8Font()
        lab.textColor = lwkA8FontColor()
        lab.numberOfLines = 0
        return lab
    }()
    
    // 打开定位权限按钮
    lazy var openLocationBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("去设置", for: .normal)
        btn.setTitleColor(lwkA20FontColor(), for: .normal)
        btn.backgroundColor = UIColor.hex(hexString: "#17191D")
        btn.layer.cornerRadius = 20
        btn.layer.masksToBounds = true
        btn.titleLabel?.font = lwkA20Font()
        btn.addTarget(self, action: #selector(locationSet), for: .touchUpInside)
        return btn
    }()
    
    lazy var closeBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "location_alert_close"), for: .normal)
        btn.addTarget(self, action: #selector(close), for: .touchUpInside)
        return btn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func close() {
        delegate?.locationViewClose()
    }
    
    // 跳转App设置
    @objc func locationSet() {
        delegate?.locationViewClose()
        let settingUrl = URL(string: UIApplication.openSettingsURLString)
        if let url = settingUrl {
            if  UIApplication.shared.canOpenURL(url){
                
                UIApplication.shared.open(url, options: [:], completionHandler: { (ist)in
                    
                })
            }
        }
    }
}
